/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Mcu_ResetDrv.c                                                                            *
 * \brief    AUTOSAR 4.3.1 MCAL Mcu Driver                                                             *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2023/08/02     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/

/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "RegHelper.h"
#include "Mcu_GeneralTypes.h"
#include "Mcu_ResetDrv.h"
#include "Mcu_Rstgen.h"
#include "Mcu_ResetSig.h"

#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"
/********************************************************************************************************
 *                                  Private Function Declarations                                       *
 *******************************************************************************************************/
/** *****************************************************************************************************
 * \brief assert/Deassert a reset signal.
 *
 * \verbatim
 * Syntax             : static Std_ReturnType Mcu_Ip_ResetDrvReleaseCtl(uint32 base, uint32 id,
                                                                        boolean release)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *                      id - reset signal id
 *                      release - TRUE: deassert, FALSE: assert
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : 0 if OK, or a error code.
 *
 * Description        : assert/Deassert a reset signal.
 *
 * \endverbatim
 *******************************************************************************************************/
static Std_ReturnType Mcu_Ip_ResetDrvReleaseCtl(uint32 base, uint32 id, boolean release)
{
    Std_ReturnType ret;

    switch (MCU_RESET_TYPE(id))
    {
    case MCU_RESET_CORE:
        ret = Mcu_Ip_RstgenLldCoreControl(base, MCU_RESET_INDEX(id), release);
        break;

    case MCU_RESET_MODULE:
        ret = Mcu_Ip_RstgenLldModuleControl(base, MCU_RESET_INDEX(id), release);
        break;

    case MCU_RESET_MISSION:
        ret = Mcu_Ip_RstgenLldMissionControl(base, MCU_RESET_INDEX(id), release);
        break;

    default:
        ret = MCU_E_PARAM_CONFIG;
        break;
    }

    return ret;
}

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
/** *****************************************************************************************************
 * \brief Assert a reset signal.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_ResetDrvAssert(uint32 base, uint32 id)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *                      id - reset signal id
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : 0 if OK, or a error code.
 *
 * Description        : Assert a reset signal.
 *
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_ResetDrvAssert(uint32 base, uint32 id)
{
    Std_ReturnType errStatus;

    errStatus = Mcu_Ip_ResetDrvReleaseCtl(base, id, FALSE);

    return errStatus;
}
/** *****************************************************************************************************
 * \brief Deassert a reset signal.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_ResetDrvDeassert(uint32 base, uint32 id)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *                      id - reset signal id
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : 0 if OK, or a error code.
 *
 * Description        : Deassert a reset signal.
 *
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_ResetDrvDeassert(uint32 base, uint32 id)
{
    Std_ReturnType errStatus;

    errStatus = Mcu_Ip_ResetDrvReleaseCtl(base, id, TRUE);

    return errStatus;
}
/** *****************************************************************************************************
 * \brief Assert a reset signal, and deassert automatically.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_ResetDrvReset(uint32 base, uint32 id)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *                      id - reset signal id
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : 0 if OK, or a error code.
 *
 * Description        : Assert a reset signal, and deassert automatically.
 *
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_ResetDrvReset(uint32 base, uint32 id)
{
    Std_ReturnType ret;

    switch (MCU_RESET_TYPE(id))
    {
    case MCU_RESET_CORE:
        ret = Mcu_Ip_RstgenLldCoreReset(base, MCU_RESET_INDEX(id));
        break;

    case MCU_RESET_MODULE:
        ret = Mcu_Ip_RstgenLldModuleControl(base, MCU_RESET_INDEX(id), FALSE);

        if (E_OK == ret)
        {
            ret = Mcu_Ip_RstgenLldModuleControl(base, MCU_RESET_INDEX(id), TRUE);
        } /* else not needed */

        break;

    case MCU_RESET_MISSION:
        ret = Mcu_Ip_RstgenLldMissionControl(base, MCU_RESET_INDEX(id), FALSE);

        if (E_OK == ret)
        {
            ret = Mcu_Ip_RstgenLldMissionControl(base, MCU_RESET_INDEX(id), TRUE);
        } /* else not needed */

        break;

    default:
        ret = MCU_E_PARAM_CONFIG;
        break;
    }

    return ret;
}

/** *****************************************************************************************************
 * \brief Global reset.
 *
 * \verbatim
 * Syntax             : void Mcu_Ip_ResetDrvGlobalReset(uint32 base)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : Global reset.
 *
 * \endverbatim
 *******************************************************************************************************/
void Mcu_Ip_ResetDrvGlobalReset(uint32 base)
{
    Mcu_Ip_RstgenLldGlobalReset(base);
}
/** *****************************************************************************************************
 * \brief Check reset signal status.
 *
 * \verbatim
 * Syntax             : uint32 Mcu_Ip_ResetDrvStatus(uint32 base, uint32 id)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : base - Reset base address.
 *                      id - reset signal id
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : 0 if deasserted, 1 if asserted,
 *                      or UINT32_MAX means error occured.
 *
 * Description        : Check reset signal status.
 *
 * \endverbatim
 * Traceability       : SW_SM006
 *******************************************************************************************************/
uint32 Mcu_Ip_ResetDrvStatus(uint32 base, uint32 id)
{
    uint32 assertStatus;

    switch (MCU_RESET_TYPE(id))
    {
    case MCU_RESET_CORE:
        assertStatus = (uint32)Mcu_Ip_RstgenLldGetCoreResetStatus(base, MCU_RESET_INDEX(id));
        break;

    case MCU_RESET_MODULE:
        assertStatus = (uint32)Mcu_Ip_RstgenLldGetModuleResetStatus(base, MCU_RESET_INDEX(id));
        break;

    case MCU_RESET_MISSION:
        assertStatus = (uint32)Mcu_Ip_RstgenLldGetMissionResetStatus(base, MCU_RESET_INDEX(id));
        break;

    default:
        assertStatus = UINT32_MAX;
        break;
    }

    if (0U == assertStatus)
    {
        assertStatus = 1U;
    }
    else if (1U == assertStatus)
    {
        assertStatus = 0U;
    }
    else
    {
        /*do nothing*/
    }

    return assertStatus;
}

#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"

/* End of file */
